package com.xiaomi.baidumap.favorite;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;


import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;

import android.widget.PopupMenu;
import android.widget.TextView;

import com.xiaomi.baidumap.R;
import com.xiaomi.baidumap.common.ActivityUtils;
import com.xiaomi.baidumap.common.LogUtils;
import com.xiaomi.baidumap.favorite.dao.DBHelper;
import com.xiaomi.baidumap.favorite.dao.LocalRepoDao;
import com.xiaomi.baidumap.favorite.dao.LocalRepoGroupDao;
import com.xiaomi.baidumap.favorite.model.LocalRepo;
import com.xiaomi.baidumap.favorite.model.RepoGroup;
import com.xiaomi.baidumap.favorite.presenter.LocalPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 16-9-1.
 */
public class FavoriteFragment extends Fragment implements PopupMenu.OnMenuItemClickListener{

    @BindView(R.id.tvGroupType)
    TextView tvGroupType;
    @BindView(R.id.btnFilter)
    ImageButton btnFilter;
    @BindView(R.id.listView)
    ListView listView;

    private LocalRepoGroupDao repoDao;
    private LocalRepoDao localRepoDao;
    private ActivityUtils activityUtils;
    private FavoriteAdapter adapter;
    private int repoGroupId = R.id.repo_group_all;
    private LocalRepo currentLocalRepo;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        repoDao = new LocalRepoGroupDao(DBHelper.getInstance(getContext()));
        localRepoDao = new LocalRepoDao(DBHelper.getInstance(getContext()));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_favorite,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        activityUtils = new ActivityUtils(this);
        adapter = new FavoriteAdapter();
        listView.setAdapter(adapter);
      //  activityUtils.showToast(localRepoDao.queryAll().size()+"");

        //默认的时候是全部数据
        setData(R.id.repo_group_all);

        //表示作用到lisgview上面
        registerForContextMenu(listView);
    }

    //按钮的点击事件
    @OnClick(R.id.btnFilter)
    public void showPopupMenu(View view){
        PopupMenu popupMenu = new PopupMenu(getContext(),view);
        //给PopupMenu填充本地Menu
        popupMenu.inflate(R.menu.menu_popup_repo_groups);
        popupMenu.setOnMenuItemClickListener(this);
        //我们自己在类别表里面其他的分类，怎么进行填充
        /**
         * 1.拿到Menu
         * 2.读取数据库的数据
         * 3.数据填充到menu上
         */
        Menu menu = popupMenu.getMenu();
        List<RepoGroup> repoGroups = repoDao.queryForAll();
        for (RepoGroup repo:repoGroups) {
            menu.add(Menu.NONE,repo.getId(),Menu.NONE,repo.getName());
        }
        popupMenu.show();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {

        tvGroupType.setText(item.getTitle());
        repoGroupId = item.getItemId();
        setData(repoGroupId);
        return true;
    }

    private void setData(int itemId) {
        switch (itemId){
            case R.id.repo_group_all:
                adapter.addData(localRepoDao.queryAll());
                break;
            case R.id.repo_group_no:
                adapter.addData(localRepoDao.queryNoGroup());
                break;
            default:
                adapter.addData(localRepoDao.queryForGroupId(repoGroupId));
                break;
        }
    }

    /*
    * contextMenu 上下文菜单
    * 1.获得上下文菜单作用到什么地方
    * 2.创建上下文菜单
    * */

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //表示的是作用到listview
        if (v.getId() == R.id.listView){

            AdapterView.AdapterContextMenuInfo adapterContextMenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
            int position = adapterContextMenuInfo.position;
            currentLocalRepo = (LocalRepo) adapter.getItem(position);
            //长按之后，显示菜单
            //获得菜单的布局，然后显示出来
            MenuInflater menuInflater = getActivity().getMenuInflater();
            menuInflater.inflate(R.menu.menu_context_favorite,menu);
            //获得子菜单，然后获得数据库中的数据填充到子菜单上面
            SubMenu subMenu = menu.findItem(R.id.sub_menu_move).getSubMenu();

            //从数据库中获得数据
            List<RepoGroup> repoGroups = repoDao.queryForAll();
            for (RepoGroup repos:repoGroups ) {
                subMenu.add(R.id.menu_group_move,repos.getId(),Menu.NONE,repos.getName());
            }
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        /*
        * 如果条目是删除就执行删除功能
        * 如果是移动至就执行其功能
        * */
        //删除功能
        if (itemId == R.id.delete){
            //获得仓库中的点击条目
            LogUtils.d(currentLocalRepo.toString());
            localRepoDao.deleteData(currentLocalRepo);
            LogUtils.d(repoGroupId + "");
            setData(repoGroupId);
            return true;
        }

        //移动至功能
        /*
        1.获得移动至的子菜单，然后根据子菜单来更新数据库中的数据
        * */
        int groupId = item.getGroupId();
        if (groupId == R.id.menu_group_move){
            if (itemId == R.id.repo_group_no){
                //移动至未分类中
                currentLocalRepo.setGroup(null);
            }else {
                //从数据中获得分类，并移动至其中
                RepoGroup repoGroup = repoDao.queryId(itemId);
                currentLocalRepo.setGroup(repoGroup);
            }
            //数据库的更新
            localRepoDao.createOrUpdate(currentLocalRepo);
            setData(repoGroupId);
            return true;
        }
        return true;
    }
}
